<?php

/**
 * My Capabilitys
 *
 * @author Álvaro Peña Cataldo
 */

class myCapability {

    private static $instance = null;
    private static $caplist = null;

    public static function setcap($flags, $cap) {
        return (int) $flags | (int) $cap;
    }

    public static function unsetcap($flags, $cap) {
        $flags = $flags + 0;
        $cap = $cap + 0;
        return $flags & ~$cap;
    }

    public static function testcap($flags, $cap) {
        $flags = (int) $flags;
        $cap = (int) $cap;

        return ($flags & $cap) == $cap;
    }

    public static function C($capname) {
        if (null == self::$caplist)
            self::__init();

        if (isset(self::$caplist[$capname]))
            return (int) self::$caplist[$capname];

        trigger_error("ERROR: CAPABILITY '$capname' NO EXISTE", E_USER_ERROR);
        return null;
    }

    private static function __init() {
        if (null != self::$caplist)
            return;
        self::$caplist = array();

        //se reemplaza por la consulta correspondiente
        $clist = Doctrine_Query::create()
                        ->from("Capabilitys c")
                        ->fetchArray();

        foreach ($clist as $c) {
            self::$caplist[$c['name']] = (int) $c['value'];
        }
    }

}

?>
